﻿Imports System
Imports Microsoft.Reporting.WebForms

Partial Class Report
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Try
                RunReport()
            Catch ex As Exception
                lblErrorMessage.Text = ex.Message
                lblErrorMessage.ToolTip = ex.StackTrace.ToString
                System.IO.File.AppendAllText(Application("ErrorFile"), vbCrLf & vbCrLf & Now() & vbCrLf & ex.ToString())
                tdErrorMessage.Style("display") = ""
            End Try
        End If

    End Sub

    Dim ReportName$ = ""

    Private Sub RunReport()

        If Not Request.QueryString.HasKeys Then Exit Sub
        ReportName$ = Request.QueryString("ReportName")

        Dim rpa() As ReportParameter = GetParameterValues()
        If Not IsNothing(rpa) Then

            Dim ieRptRows As IEnumerable = GetReportData(rpa)
            Dim rds As ReportDataSource = New ReportDataSource("DataSet1", ieRptRows)

            With ReportViewer1
                .Reset()
                .ProcessingMode = ProcessingMode.Local
                With .LocalReport
                    .EnableHyperlinks = True
                    .ReportPath = "reports\SSRS\" & ReportName & ".rdlc"
                    .SetParameters(rpa)
                    .DataSources.Clear()
                    .DataSources.Add(rds)
                    .Refresh()
                End With
            End With

        End If
    End Sub

    Protected Friend Function GetReportData(ByVal pRpa() As ReportParameter) As IEnumerable
        With srcRpt
            .SelectCommand = SQLFiles.ConstructSQL(ReportName, getCookie("IsAcctMgr"))

            For i As Int16 = 0 To pRpa.Length - 1
                .SelectParameters(i).DefaultValue = pRpa(i).Values(0)
            Next

        End With
        Return srcRpt.Select(DataSourceSelectArguments.Empty)
    End Function

    Function GetParameterValues() As ReportParameter()

        If IsNothing(Application("CacheDate")) Then Return Nothing

        Dim FromDate$ = getCookie("FromDate", Month(Application("CacheDate")) & "/10/" & Year(Application("CacheDate")))
        Dim ToDate$ = getCookie("ToDate", Application("CacheDate"))

        Dim Region$ = StrConv(getCookie("RegionMarket", "All Regions"), vbProperCase)
        If Not Region.Contains(":") Then Region &= ":1"

        Dim AcctMgr$ = StrConv(getCookie("AcctMgr").Split(":")(0), vbProperCase)
        If getCookie("IsAcctMgr") = "%" Then
            AcctMgr = "%"
            Region = "%:0"
        ElseIf CBool(getCookie("IsAcctMgr")) = True Then
            Region = "%:0"
        Else
            AcctMgr = "%"
        End If

        Dim National$ = getCookie("National").Replace("Both", "%")
        Dim Dwelling$ = getCookie("Dwelling").Replace("Both", "%")
        Dim LineOfBusiness$ = getCookie("LineOfBusiness").Replace("All", "%")

        Dim ByProductOrChannel$ = getCookie("ByProductOrChannel")

        Dim rpa() As ReportParameter = _
        { _
        New ReportParameter("CacheDate", Application("CacheDate").ToString()), _
        New ReportParameter("Region", Region), _
        New ReportParameter("From", FromDate), _
        New ReportParameter("To", ToDate), _
        New ReportParameter("AcctMgr", AcctMgr), _
        New ReportParameter("National", National), _
        New ReportParameter("Dwelling", Dwelling), _
        New ReportParameter("LineOfBusiness", LineOfBusiness), _
        New ReportParameter("ByProductOrChannel", ByProductOrChannel) _
        }
        Return rpa

    End Function


    Protected Function getCookie$(ByVal psCookieName As String, Optional ByVal psDefault$ = "%")
        Dim sCookie As String = psCookieName + "_Report"
        If Request.Cookies(sCookie) Is Nothing Then
            Return psDefault
        Else
            Return Server.UrlDecode(Request.Cookies(sCookie).Value)
        End If
    End Function

    Protected Sub srcRpt_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles srcRpt.Selecting
        e.Command.CommandTimeout = 300
    End Sub
End Class
